require "ActionUtil"
--require "MainScene"
require "Hero"
require "FightData"
require "WarWinScene"

WarScene={}
WarScene.leftPos =
                      {[1]={75, 475},[4]={175, 475},[7]={275, 475},
                      [2]={75, 325},[5]={175, 325},[8]={275, 325},
                      [3]={75, 175},[6]={175, 175},[9]={275, 175}}
WarScene.rightPos =
                      {[1]={960-275, 475},[4]={960-175, 475},[7]={960-75, 475},
                      [2]={960-275, 325},[5]={960-175, 325},[8]={960-75, 325},
                      [3]={960-275, 175},[6]={960-175, 175},[9]={960-75, 175}}
WarScene.leftFighters = {}
WarScene.rightFighters = {}
--[[FightView.rightPos =
                      {{75, 475},{175, 475},{275, 475}}
                      {{75, 325},{175, 325},{275, 325}}
                      {{75, 175},{175, 175},{275, 175}}
]]

--1|A|5|2|NN382|(),();1|D|2|5|NB64;1|A|5|2|BN181|(),();1|A|8|2|ND562|(),();1|D|3|5|NN107|(),();2|AS|5|0|P|(45,46,47);2|A|5|3|PN463|(45,46,47),();2|D|3|5|NN79|(),(45,46,47);2|A|8|3|ND605|(),()

local SoldierName = {[81]="qingtan", [11]="raoluanche", [42]="zhongtan", [49]="zhishengji"}

local NodeID = {HeadBk=1001, UpCost=1002}
local LabelState = {LabelScaled=1, LabelFaded=2}
local HeroState = {HeroHurt=1, HeroDead=2}

WarScene.hero = "hero70"
WarScene.attackHeroName = "hero58"
WarScene.defendHeroName = "hero70"

WarScene.getNode = function()
  WarScene.node = CCBReader:load("WarScene.ccbi")
  print("WarScene.getNode", WarScene.node)
  WarScene.init() 
  WarScene.playNext()
  return WarScene.node
end

WarScene.getScene = function()
  local sc = CCScene:create()
  sc:addChild(WarScene.getNode())  
  print("WarScene.getScene", sc)
  return sc
end

WarScene.init = function()
  FightData.load()
  WarScene.fightSeq = FightData.fightSeq
  print("WarScene.init WarScene.fightSeq", WarScene.fightSeq, WarScene.fightSeq:size())
  WarScene.fightIndex = 0
  for k,v in pairs(FightData.leftArmy) do
    local pos = WarScene.leftPos[v.location]
    local name = SoldierName[v.soldier_id]
    print(v.soldier_id, v.location,  name, pos[1], pos[2])
    local h = Hero:create(WarScene.node, name, ccp(pos[1],pos[2]))
    h.pos = v.location
    WarScene.leftFighters[v.location] = h
    print("WarScene.init left", name, v.location)
  end
  for k,v in pairs(FightData.rightArmy) do
    local pos = WarScene.rightPos[v.location]
    local name = SoldierName[v.soldier_id]
    print(v.soldier_id, v.location, name, pos[1], pos[2])
    local h = Hero:create(WarScene.node, name, ccp(pos[1],pos[2]))
    h.normalView:setFlipX(true)
    h.pos = v.location
    WarScene.rightFighters[v.location] = h
    print("WarScene.init right", name, v.location)
  end
end

WarScene.getAttacker = function(fight)
  print("WarScene.getAttacker", fight.direction, fight.attackPos)
  if fight.direction=="A" then
    return WarScene.leftFighters[fight.attackPos]
  else
    return WarScene.rightFighters[fight.attackPos]
  end
end

WarScene.getDefenders = function(fight)
  local arr = Array:create()
  for i=0, fight.defend:size()-1 do
    if fight.direction=="A" then
      arr:append(WarScene.rightFighters[fight.defend:at(i).defendPos])
    else
      arr:append(WarScene.leftFighters[fight.defend:at(i).defendPos])
    end
  end
  return arr
end

WarScene.endFight = function()
  print("WarScene.endFight")
  CCDirector:sharedDirector():replaceScene(CCTransitionSlideInR:create(1,WarWinScene.getScene()))
end

WarScene.playNext = function()
  local aFight = WarScene.fightSeq:at(WarScene.fightIndex)
  local h = WarScene.getAttacker(aFight)
  local defenders = WarScene.getDefenders(aFight) 
  
  print("WarScene.playNext", WarScene.fightIndex, h)
  if defenders:size()>0 then
    h.setState(Hero.HeroState.Fire) 
    WarScene.waitCount = aFight.defend:size()
  else
    --buf
    --local ac = ActionUtil.createAction("tiebi.plist", 0.1, "")
    --local sp = CCSprite:createWithSpriteFrameName("tiebi_tiebi0001.png")
    --sp:setPosition(h.normalView:getPosition())
    --h.normalView:getParent():addChild(sp)
    --sp:runAction(CCRepeatForever:create(ac)) 
    
    --h.normalView:runAction(ActionUtil.createSeqAction({CCDelayTime:create(0.2), CCCallFuncN:create(onAction)}))
    --print("WarScene.playNext play buf", aFight.attackBuf)
    --WarScene.waitCount = 0 
  end
end

function onRemoveFromParent(node)
  node:getParent():removeChild(node, true)
end

function onHeroStateChanged = function(hero)
  local aFight = WarScene.fightSeq:at(WarScene.fightIndex)
  local attacker = WarScene.getAttacker(aFight)
  local defenders = WarScene.getDefenders(aFight) 
  if hero.currentState==Hero.State.Fire then
    hero:setHeroState(Hero.HeroState.Normal)
    for i=0,defenders:size()-1 do
      defenders:at(i):setHeroState(Hero.State.Hurt)
    end 
  elseif hero.currentState==Hero.State.Hurt then
    hero:setHeroState(Hero.HeroState.Normal)
    local state = aFight.defend:at(hero.pos).defendType
    local buf = aFight.defend:at(hero.pos).buf
    print("onAction defend end", WarScene.fightIndex, hero.pos, state, buf)
    if state=="D" then
      hero:release()
    else
      if buf=="" then end
    end
    
    if tostring(hero) == tostring(defenders:at(0)) then
      WarScene.fightIndex = WarScene.fightIndex+1
      print("WarScene.onAction begin next fight", WarScene.fightIndex)
      if WarScene.fightIndex < WarScene.fightSeq:size() then
        WarScene.playNext()
      else
        WarScene.endFight()
      end
    end
  end
end

function closeWarScene(node)
  CCDirector:sharedDirector():replaceScene(CCTransitionSlideInR:create(1,MainScene.getScene()))
end


